package com.atguigu.crowd.mvc.controller;

import com.atguigu.crowd.entity.Auth;
import com.atguigu.crowd.service.api.AuthService;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author zzqstart
 * @Description
 * @date 2022-09-06 22:14
 */
@RestController
public class AuthController {
    @Autowired
    private AuthService authService;

    @RequestMapping("auth/root/save.json")
    public ResultEntity<String> saveRootAuth(Auth auth) {
        authService.saveRootAuth(auth);
        return ResultEntity.successWithoutData();
    }


    @RequestMapping("auth/remove.json")
    public ResultEntity<String> removeAuth(Auth auth) {
        authService.removeAuth(auth);
        return ResultEntity.successWithoutData();

    }

    @RequestMapping("auth/edit.json")
    public ResultEntity<String> editAuth(Auth auth) {
        authService.editAuth(auth);
        return ResultEntity.successWithoutData();

    }

    @RequestMapping("auth/save.json")
    public ResultEntity<String> saveAuth(Auth auth) {
        authService.saveAuth(auth);
        return ResultEntity.successWithoutData();
    }

    @RequestMapping("assign/do/role/assign/auth.json")
    public ResultEntity<String> saveRoleAssignAuth(
            @RequestBody Map<String, List<Integer>> map
    ) {
        authService.saveRoleAssignAuth(map);
        return ResultEntity.successWithoutData();
    }

    @RequestMapping("assign/get/assigned/auth/id/by/role/id.json")
    public ResultEntity<List<Integer>> getAuthByRoleId(@RequestParam("roleId") Integer roleId) {
        List<Integer> authIdList = authService.getAuthByRoleId(roleId);
        return ResultEntity.successWithData(authIdList);
    }

    @RequestMapping("auth/get/whole/tree.json")
    public ResultEntity<List<Auth>> getAllAuth() {
        List<Auth> authList = authService.getAllAuth();
        return ResultEntity.successWithData(authList);
    }
}
